<?php
namespace app\admin\model;

use think\Model;

class Settlement extends Model{

    public function getAll(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
              $where['settlement.order_id|user.full_name'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $settlement = $this
            ->alias("settlement")
            ->join('order','settlement.order_id = order.id','LEFT')
            ->join('lesson','order.lesson_id = lesson.id','LEFT')
            ->join('user','order.user_id = user.id','LEFT')
            ->where('settlement.settlement_status=0')
            ->where($where)
            ->field('settlement.id,settlement.order_id,order_sn,lesson_id,
                    lesson_name,full_name as order_name,order.user_id,
                     settlement_status,settlement_lesson_num')
            ->select();
        if($settlement){
            foreach($settlement as $k => $v){
                $settlement[$k] = $v->toArray();
            }
        }
        return $settlement;

    }

    public  function getCount(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['settlement.order_id|user.full_name'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $count = $this
            ->alias("settlement")
            ->join('order','settlement.order_id = order.id','LEFT')
            ->join('lesson','order.lesson_id = lesson.id','LEFT')
            ->join('user','order.user_id = user.id','LEFT')
            ->where($where)
            ->where('settlement.settlement_status=0')
            ->field('settlement.id,settlement.order_id,order_sn,order.user_id,
                    lesson_id,lesson_name,full_name as order_name,
                     settlement_status,settlement_lesson_num')
            ->count();
        return $count;
    }

    public function getOne($id){
        $where = "settlement.id=".$id;
        $settlement = $this
            ->alias("settlement")
            ->join('order','settlement.order_id = order.id','LEFT')
            ->join('lesson','order.lesson_id = lesson.id','LEFT')
            ->join('user order_user','order.user_id = order_user.id','LEFT')
            ->join('user rec_student','settlement.rec_student_user = rec_student.id','LEFT')
            ->join('user rec_teacher','settlement.rec_teacher_user = rec_teacher.id','LEFT')
            ->join('user rec_classroom','settlement.rec_classroom_user = rec_classroom.id','LEFT')
            ->join('user student_user','settlement.student_user = student_user.id','LEFT')
            ->join('user teacher_user','settlement.teacher_user = teacher_user.id','LEFT')
            ->join('user cooperate_user','settlement.cooperate_user = cooperate_user.id','LEFT')
            ->join('user material_user','settlement.material_user = material_user.id','LEFT')
            ->join('user education_user','settlement.education_user = education_user.id','LEFT')
            ->join('user reclass_user','settlement.reclass_user = reclass_user.id','LEFT')
            ->join('user classroom_owner_user','settlement.classroom_owner_user = classroom_owner_user.id','LEFT')
            ->join('user total_rec_user','settlement.total_rec_user = total_rec_user.id','LEFT')
            ->join('user province_rec_user','settlement.province_rec_user = province_rec_user.id','LEFT')
            ->join('user city_rec_user','settlement.city_rec_user = city_rec_user.id','LEFT')
            ->join('user area_rec_user','settlement.area_rec_user = area_rec_user.id','LEFT')
            ->join('user school_rec_user','settlement.school_rec_user = school_rec_user.id','LEFT')
            ->join('user special_user1','settlement.special_user1 = special_user1.id','LEFT')
            ->join('user special_user2','settlement.special_user2 = special_user2.id','LEFT')
            ->join('user special_user3','settlement.special_user3 = special_user3.id','LEFT')
            ->join('user special_user4','settlement.special_user4 = special_user4.id','LEFT')
            ->join('user special_user5','settlement.special_user1 = special_user5.id','LEFT')
            ->where($where)
            ->field('settlement.id,settlement.order_id,order_sn,lesson_id,lesson_name,order_user.full_name as order_name,
                    rec_student.full_name as rec_student_user,settlement.rec_student_bonus,
                    rec_teacher.full_name as rec_teacher_user,settlement.rec_teacher_bonus,
                    rec_classroom.full_name as rec_classroom_user,settlement.rec_classroom_bonus,
                    student_user.full_name as student_user,settlement.student_tuition,
                    teacher_user.full_name as teacher_user,settlement.teacher_bonus,settlement.teacher_settlement_ratio,
                    cooperate_user.full_name as cooperate_user,settlement.cooperate_bonus,
                    material_user.full_name as material_user,settlement.material_bonus,
                    education_user.full_name as education_user,settlement.education_bonus,
                    reclass_user.full_name as reclass_user,settlement.reclass_bonus,
                    classroom_owner_user.full_name as classroom_owner_user,settlement.classroom_owner_bonus,
                    total_rec_user.full_name as total_rec_user,settlement.total_rec_bonus,
                    province_rec_user.full_name as province_rec_user,settlement.province_rec_bonus,
                    city_rec_user.full_name as city_rec_user,settlement.city_rec_bonus,
                    area_rec_user.full_name as area_rec_user,settlement.area_rec_bonus,
                    school_rec_user.full_name as school_rec_user,settlement.school_rec_bonus,
                    special_user1.full_name as special_user1,settlement.special_user1_bonus,
                    special_user2.full_name as special_user2,settlement.special_user2_bonus,
                    special_user3.full_name as special_user3,settlement.special_user3_bonus,
                    special_user4.full_name as special_user4,settlement.special_user4_bonus,
                    special_user5.full_name as special_user5,settlement.special_user5_bonus,
                    settlement.settlement_escape_lesson_num,
                     settlement_status,settlement_lesson_num')
            ->find();
        if($settlement){
            $settlement = $settlement->toArray();
        }
        return $settlement;
    }

    public function getAllHadSettlement(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['settlement.order_id|user.full_name'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $settlement = $this
            ->alias("settlement")
            ->join('order','settlement.order_id = order.id','LEFT')
            ->join('lesson','order.lesson_id = lesson.id','LEFT')
            ->join('user','order.user_id = user.id','LEFT')
            ->where($where)
            ->where('settlement.settlement_status=1')
            ->field('settlement.id,settlement.order_id,is_grant,order_sn,
                    lesson_id,lesson_name,full_name as order_name,order.user_id,
                     settlement_status,settlement_lesson_num')
            ->select();
        if($settlement){
            foreach($settlement as $k => $v){
                $settlement[$k] = $v->toArray();
            }
        }
        return $settlement;
    }

    public function getCountHadSettlement(){
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['settlement.order_id|user.full_name'] = array('like' ,"%{$keywords}%" );
        }else{
            $where=[];
        }
        $count = $this
            ->alias("settlement")
            ->join('order','settlement.order_id = order.id','LEFT')
            ->join('lesson','order.lesson_id = lesson.id','LEFT')
            ->join('user','order.user_id = user.id','LEFT')
            ->where($where)
            ->where("settlement.settlement_status=1")
            ->field('settlement.id,settlement.order_id,is_grant,order_sn,
                    lesson_id,lesson_name,full_name as order_name,
                     settlement_status,settlement_lesson_num,order.user_id')
            ->count();
        return $count;
    }

    public function getOneHadSettlement($id){
        $where = "settlement.id=".$id;
        $settlement = $this
            ->alias("settlement")
            ->join('order','settlement.order_id = order.id','LEFT')
            ->join('lesson','order.lesson_id = lesson.id','LEFT')
            ->join('user order_user','order.user_id = order_user.id','LEFT')
            ->join('user rec_student','settlement.rec_student_user = rec_student.id','LEFT')
            ->join('user rec_teacher','settlement.rec_teacher_user = rec_teacher.id','LEFT')
            ->join('user rec_classroom','settlement.rec_classroom_user = rec_classroom.id','LEFT')
            ->join('user student_user','settlement.student_user = student_user.id','LEFT')
            ->join('user teacher_user','settlement.teacher_user = teacher_user.id','LEFT')
            ->join('user cooperate_user','settlement.cooperate_user = cooperate_user.id','LEFT')
            ->join('user material_user','settlement.material_user = material_user.id','LEFT')
            ->join('user education_user','settlement.education_user = education_user.id','LEFT')
            ->join('user reclass_user','settlement.reclass_user = reclass_user.id','LEFT')
            ->join('user classroom_owner_user','settlement.classroom_owner_user = classroom_owner_user.id','LEFT')
            ->join('user total_rec_user','settlement.total_rec_user = total_rec_user.id','LEFT')
            ->join('user province_rec_user','settlement.province_rec_user = province_rec_user.id','LEFT')
            ->join('user city_rec_user','settlement.city_rec_user = city_rec_user.id','LEFT')
            ->join('user area_rec_user','settlement.area_rec_user = area_rec_user.id','LEFT')
            ->join('user school_rec_user','settlement.school_rec_user = school_rec_user.id','LEFT')
            ->join('user special_user1','settlement.special_user1 = special_user1.id','LEFT')
            ->join('user special_user2','settlement.special_user2 = special_user2.id','LEFT')
            ->join('user special_user3','settlement.special_user3 = special_user3.id','LEFT')
            ->join('user special_user4','settlement.special_user4 = special_user4.id','LEFT')
            ->join('user special_user5','settlement.special_user1 = special_user5.id','LEFT')
            ->where($where)
            ->where("settlement.settlement_status=1")
            ->field('settlement.id,settlement.order_id,order_sn,lesson_id,lesson_name,order_user.full_name as order_name,
                    rec_student.full_name as rec_student_user,settlement.rec_student_bonus,
                    rec_teacher.full_name as rec_teacher_user,settlement.rec_teacher_bonus,
                    rec_classroom.full_name as rec_classroom_user,settlement.rec_classroom_bonus,
                    student_user.full_name as student_user,settlement.student_tuition,
                    teacher_user.full_name as teacher_user,settlement.teacher_bonus,settlement.teacher_settlement_ratio,
                    cooperate_user.full_name as cooperate_user,settlement.cooperate_bonus,
                    material_user.full_name as material_user,settlement.material_bonus,
                    education_user.full_name as education_user,settlement.education_bonus,
                    reclass_user.full_name as reclass_user,settlement.reclass_bonus,
                    classroom_owner_user.full_name as classroom_owner_user,settlement.classroom_owner_bonus,
                    total_rec_user.full_name as total_rec_user,settlement.total_rec_bonus,
                    province_rec_user.full_name as province_rec_user,settlement.province_rec_bonus,
                    city_rec_user.full_name as city_rec_user,settlement.city_rec_bonus,
                    area_rec_user.full_name as area_rec_user,settlement.area_rec_bonus,
                    school_rec_user.full_name as school_rec_user,settlement.school_rec_bonus,
                    special_user1.full_name as special_user1,settlement.special_user1_bonus,
                    special_user2.full_name as special_user2,settlement.special_user2_bonus,
                    special_user3.full_name as special_user3,settlement.special_user3_bonus,
                    special_user4.full_name as special_user4,settlement.special_user4_bonus,
                    special_user5.full_name as special_user5,settlement.special_user5_bonus,
                    settlement.settlement_escape_lesson_num,
                     settlement_status,settlement_lesson_num')
            ->find();
        if($settlement){
            $settlement = $settlement->toArray();
        }
        return $settlement;
    }

}